﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using TechPath.DCM.Sharepoint.Services.Util;

namespace TechPath.DCM.Sharepoint.Services.Data
{
    internal static class DeepZoomDataAccess
    {

        public static DzImage GetImageBySourceImageKey(string sourceImageKey)
        {
            DzImage retVal = null;

            using (SqlConnection conn = new SqlConnection(Config.DbConnectionString))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "dbo.GetImage";
                cmd.Parameters.AddWithValue("@SourceImageKey", sourceImageKey);
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        retVal = new DzImage();
                        retVal.SourceImageKey = reader["SourceImageKey"].ToString();
                        retVal.SourceImageUri = reader["SourceImageUri"].ToString();
                        retVal.PublicDzImageUri = reader["PublicDzImageUri"].ToString();
                        retVal.PrivateDzImageUri = reader["PrivateDzImageUri"].ToString();
                        retVal.LastUpdated = reader.GetDateTime(reader.GetOrdinal("LastUpdated"));
                    }
                }
                conn.Close();
            }
            return retVal;
        }

        public static void SaveImage(DzImage image)
        {
            using (SqlConnection conn = new SqlConnection(Config.DbConnectionString))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "dbo.SaveImage";
                cmd.Parameters.AddWithValue("@SourceImageUri", image.SourceImageUri);
                cmd.Parameters.AddWithValue("@SourceImageKey", image.SourceImageKey);
                cmd.Parameters.AddWithValue("@PublicDzImageUri", image.PublicDzImageUri);
                cmd.Parameters.AddWithValue("@PrivateDzImageUri", image.PrivateDzImageUri);
                cmd.Parameters.AddWithValue("@LastUpdated", image.LastUpdated);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
    }
}
